package com.chinaunicom.behavior.responsibilitychain;

import java.util.Iterator;
import java.util.ServiceLoader;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2022/6/8 15:08
 * Description:
 */
public class Application {
    public static void main(String[] args) {
        
        // 根据 url || path 获取职责链
        HandlerExecutionChain chain = getHandler();
        
        // 获取执行器适配器
        // HandlerAdapter handlerAdapter = getHanderAdapter(chain.getHandler());

        // 执行拦截器前置处理器
        if(chain.applyPreHandler()){
            System.out.println("拦截器前置处理器执行完毕。。。。");
        }
        
        // 执行后置处理器
        chain.applyPostHandler();
    }

    
    // 获取执行器适配器
    private static HandlerAdapter getHandlerAdapter(Object handler) {
        return null;
    }


    // 根据条件获取执行器链
    private static HandlerExecutionChain getHandler() {
        try {
            HandlerExecutionChain handlerExecutionChain = new HandlerExecutionChain();
            ServiceLoader<HandlerInterceptor> load = ServiceLoader.load(HandlerInterceptor.class);
            Iterator<HandlerInterceptor> iterator = load.iterator();
            while (iterator.hasNext()) {
                HandlerInterceptor next = iterator.next();
                Class<? extends HandlerInterceptor> aClass = next.getClass();
                    HandlerInterceptor handlerInterceptor1 = aClass.newInstance();
                    handlerExecutionChain.addInterceptor(handlerInterceptor1);
            }

//        handlerExecutionChain.addInterceptor(new CustomHandlerInterceptor());
//        handlerExecutionChain.addInterceptor(new CustomWebHandlerInterceptor());

            return handlerExecutionChain;
        }catch (Exception e){
            return null;
        }
    }
}
